package cn.edu.sjtu.se.dslab.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.sjtu.se.dslab.dao.IEntityZoneCategoryDao;
import cn.edu.sjtu.se.dslab.dao.IEntityZoneDao;
import cn.edu.sjtu.se.dslab.dao.IProjectDao;
import cn.edu.sjtu.se.dslab.project.EntityZone;
import cn.edu.sjtu.se.dslab.project.EntityZoneCategory;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.service.IEntityZoneCategoryService;

@Service("entityZoneCategoryService")
@Transactional
public class EntityZoneCategoryServiceImpl implements IEntityZoneCategoryService {

	@Resource(name = "entityZoneCategoryDao")
	private IEntityZoneCategoryDao entityZoneCategoryDao;
	
	@Resource(name="projectDao")
	private IProjectDao projectDao;
	
	@Resource(name="entityZoneDao")
	private IEntityZoneDao entityZoneDao;
	
	
	@Override
	public boolean addEntityZoneCategory(EntityZoneCategory entityZoneCategory, long projectId) {
		Project project = projectDao.queryById(projectId);
		if (project == null) throw new NullPointerException("Project id not found " + projectId);
		entityZoneCategory.setProject(project);
		entityZoneCategoryDao.save(entityZoneCategory);
		return true;
	}

	@Override
	public EntityZoneCategory updateEntityZoneCategory(EntityZoneCategory entityZoneCategory) {
		return entityZoneCategoryDao.update(entityZoneCategory);
	}
	
	@Override
	public EntityZoneCategory getEntityZoneCategoryById(long id) {
		return entityZoneCategoryDao.queryById(id);
	}

	@Override
	public EntityZoneCategory getEntityZoneCategoryByName(String name) {
		return entityZoneCategoryDao.queryByName(name);
	}

	@Override
	public List<EntityZoneCategory> getEntityZoneCategoryList() {
		return entityZoneCategoryDao.queryByList();
	}

	@Override
	public boolean deleteEntityZoneCategoryById(long entityZoneCategoryId) {
		EntityZoneCategory entityZoneCategory = entityZoneCategoryDao.queryById(entityZoneCategoryId);
		List<EntityZone> entityZones = entityZoneDao.queryByCategory(entityZoneCategoryId);
		if (entityZones != null && entityZones.size() != 0) return false;
		if (null != entityZoneCategory){
			entityZoneCategoryDao.deleteEntityZoneCategory(entityZoneCategory);
			return true;
		}
		return false;
	}

	@Override
	public List<EntityZoneCategory> getEntityZoneCategoryByProject(long projectId) {
		return entityZoneCategoryDao.queryByProject(projectId);
	}
	
	@Override
	public EntityZoneCategory getEntityZoneCategoryByProjectAndName(long projectId, String name){
		return entityZoneCategoryDao.queryByProjectAndName(projectId, name);
	}




}
